//
//  OMDeliveryAddressSelectionViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/26.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMDeliveryAddressSelectionViewController.h"
#import "BaseTableCell.h"
#import "KTTopControlBar.h"
@implementation OMAddressItem

+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"id":@"id",
             @"projectName":@"name",
             @"useArea":@"use_area",
             };
}
@end

@interface OMDeliveryAddressSelectionViewController () <KTTopControlBarDelegate>
@property (nonatomic, strong) NSArray <OMAddressItem *>*addresses;
@property (nonatomic, strong) NSMutableArray <OMAddressItem *>*selectedAddresses;
@end

@implementation OMDeliveryAddressSelectionViewController
KTTopControlBar* topBar = nil;
- (void)viewDidLoad {
    [super viewDidLoad];

    self.selectedAddresses = [NSMutableArray array];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = SM_SIZE(95);
    topBar = [KTTopControlBar customeTopControllBar];
    topBar.delegate = self;
    [topBar crateTopControlBtn:@"位置"];
    [self.view addSubview:topBar];

    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBar.mas_bottom);
        make.right.left.bottom.equalTo(@0);
    }];

    [self getAddressesWithParentId:nil];
}

- (void)getAddressesWithParentId:(NSString *)parentId
{
    [OMHUD HUDShieldLoading:nil];
    [KTHttpUtils GET:@"receiver/selAddress"
          parameters:@{@"parentId":parentId?parentId:@""}
            progress:nil
          completion:^(id responseObject, BOOL isSuccess, NSString *message) {
              if (isSuccess) {
                  self.addresses = [OMAddressItem mj_objectArrayWithKeyValuesArray:responseObject[OMDataKey]];
                  [OMHUD HUDHide];
                  [self.tableView reloadData];
              }else {
                  [OMHUD HUDError:message];
              }

          }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addresses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableCell *cell = [BaseTableCell tableViewCellWithTableView:tableView];
    cell.textLabel.text = self.addresses[indexPath.row].projectName;
    cell.textLabel.font = SM_CFont(30);
    cell.accessoryType = self.addresses[indexPath.row].hasChild ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMAddressItem *address = self.addresses[indexPath.row];
    [self.selectedAddresses addObject:address];

    if (address.hasChild) {
        [self getAddressesWithParentId:[self.selectedAddresses lastObject].id];
        [topBar crateTopControlBtn:address.projectName];
    }else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(projectAddressSelectionVCtrl:didSelectLastAddress:)]) {
            [self.delegate projectAddressSelectionVCtrl:self didSelectLastAddress:self.selectedAddresses.lastObject];
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(projectAddressDidReverseWithTotalAddress:)]) {
            NSMutableString *addressStr = [[NSMutableString alloc]initWithString:@""];
            for (OMAddressItem *item in self.selectedAddresses) {
                [addressStr appendString:item.projectName];
            }
            [self.delegate projectAddressDidReverseWithTotalAddress:addressStr];
        }
        [self backAction];
    }

}

#pragma mark - KTTopControlBarDelegate
- (void)controlBar:(KTTopControlBar *)controlBar didSelectButtonAtIndex:(NSUInteger)index
{
    if (index) {
        [self.selectedAddresses removeObjectsInRange:NSMakeRange(index, self.selectedAddresses.count-index)];
    }else {
        [self.selectedAddresses removeAllObjects];
    }
    [self getAddressesWithParentId:[self.selectedAddresses lastObject].id];
}




@end
